package com.graduation.springbootinit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduation.springbootinit.common.ErrorCode;
import com.graduation.springbootinit.exception.BusinessException;
import com.graduation.springbootinit.mapper.UserPointsMapper;
import com.graduation.springbootinit.model.entity.ActivityPart.PointsRecord;
import com.graduation.springbootinit.model.entity.ActivityPart.UserPoints;
import com.graduation.springbootinit.service.PointsRecordService;
import com.graduation.springbootinit.service.UserPointsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class UserPointsServiceImpl extends ServiceImpl<UserPointsMapper, UserPoints>
        implements UserPointsService {

    @Resource
    private PointsRecordService pointsRecordService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addPoints(Long userId, Integer points, String description) {
        if (points <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "积分数量必须大于0");
        }
        
        // 获取或创建用户积分账户
        UserPoints userPoints = getUserPointsAccount(userId);
        
        // 更新积分
        userPoints.setPoints(userPoints.getPoints() + points);
        updateById(userPoints);
        
        // 记录积分变动
        PointsRecord record = new PointsRecord();
        record.setUserId(userId);
        record.setPoints(points);
        record.setType(4); // 其他类型
        record.setDescription(description);
        pointsRecordService.save(record);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deductPoints(Long userId, Integer points, String description) {
        if (points <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "积分数量必须大于0");
        }
        
        // 获取用户积分账户
        UserPoints userPoints = getUserPointsAccount(userId);
        
        // 检查积分是否足够
        if (userPoints.getPoints() < points) {
            return false;
        }
        
        // 扣减积分
        userPoints.setPoints(userPoints.getPoints() - points);
        updateById(userPoints);
        
        // 记录积分变动
        PointsRecord record = new PointsRecord();
        record.setUserId(userId);
        record.setPoints(-points); // 负数表示扣减
        record.setType(3); // 兑换礼品
        record.setDescription(description);
        pointsRecordService.save(record);
        
        return true;
    }

    @Override
    public Integer getUserPoints(Long userId) {
        UserPoints userPoints = getUserPointsAccount(userId);
        return userPoints.getPoints();
    }

    @Override
    public void initUserPoints(Long userId) {
        // 检查是否已存在积分账户
        QueryWrapper<UserPoints> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        if (count(queryWrapper) > 0) {
            return;
        }
        
        // 创建新的积分账户
        UserPoints userPoints = new UserPoints();
        userPoints.setUserId(userId);
        userPoints.setPoints(0);
        save(userPoints);
    }

    /**
     * 获取用户积分账户，如果不存在则创建
     */
    private UserPoints getUserPointsAccount(Long userId) {
        QueryWrapper<UserPoints> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        UserPoints userPoints = getOne(queryWrapper);
        
        if (userPoints == null) {
            // 创建新的积分账户
            userPoints = new UserPoints();
            userPoints.setUserId(userId);
            userPoints.setPoints(0);
            save(userPoints);
        }
        
        return userPoints;
    }
} 